#-*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Number：     016
# Name:        Python of switch achieve
# Author:      SUMMVING
# Email:       summving@gmail.com
# Created:     2013/08/18  20:02
# Copyright:   (c) SUMMVING 2013
# Licence:     GPLv3
#-------------------------------------------------------------------------------

"""python里面没有switch语句，要实现可用字典"""

"""一个小计算器的例子"""

def jia(x,y):
    return x+y

def jian(x,y):
    return x-y

def cheng(x,y):
    return x*y

def chu(x,y):
    return x/y

operator = {"+":jia,"-":jian,"*":cheng,"/":chu}   #建立字典，这里还可以更简便，把上面的定义写进去
print operator["*"](11,12)                        #等效于print cheng(11,12)；比判断语句效率高

def f(x,o,y):                                     #以定义函数取值的方式更直观，结果一样
    print operator.get(o)(x,y)                    #从operator里取值，再计算

f(11,"*",12)


